﻿using System;
using System.Collections.Generic;

namespace Neitzel
{
    /// <summary>
    /// Array extensions.
    /// </summary>
    public static class ArrayExtensions
    {
        /// <summary>
        /// Compares to Lists. The order must be the same in both lists.
        /// </summary>
        /// <typeparam name="T">Type stored inside the list.</typeparam>
        /// <param name="one">First list.</param>
        /// <param name="two">Second list.</param>
        /// <returns>True if the elements in the two lists are the same, else false.</returns>
        public static bool Equals<T>(this IList<T> one, IList<T> two)
        {
            // validate
            if (one == null) throw new ArgumentNullException(nameof(one));

            // if second argument is null, then elements are not equal.
            if (two == null) return false;

            // Compare count.
            if (one.Count != two.Count)
                return false;

            // compare all elements.
            for (int i = 0; i < one.Count; i++)
                if (!two[i].Equals(one[i])) return false;

            // return that lists are equal.
            return true;
        }

        /// <summary>
        /// Calculates the Hashcode of all elements inside an list.
        /// </summary>
        /// <typeparam name="T">Type used inside the list.</typeparam>
        /// <param name="list">List to calculate the hashcode on.</param>
        /// <returns>Hashcode of all elements inside the list.</returns>
        public static int GetHashCode<T>(this IList<T> list)
        {
            // validation
            if (list == null) throw new ArgumentNullException(nameof(list));

            // variable for hashcode
            int hashCode = 0;

            // calculate hashcode for each element.
            foreach (T element in list)
                hashCode ^= element.GetHashCode();

            // return hashcode.
            return hashCode;
        }
    }
}
